<?php namespace app\api\controller;

use app\common\controller\AuthAndBind;
use think\Controller;
use think\Db;
use alisms\SendSms;
use esign\EsignHandle;

/**
 * api
 */
class Auction extends AuthAndBind{
	
	public function __construct(){
		parent::__construct();
	}
	
	/**
	 * 保存收货信息
	 */
	public function saveReceivingInfo(){
		if(request() -> isPost()){
			$in = input("post.");
			$bool = is_empty(['user_id', 'id', 'receiving_address', 'receiving_name', 'receiving_phone'], $in);
			if($bool){
				$outArr['code'] = '40004';
			}else{
				$userInfo = $this -> getUserInfo();
				
				$where[] = ['id', 'eq', $in['id']];
				$auction = Db::table("gd_auction") -> where($where) -> find();
				if( empty($auction) ) returnResults(['code'=>'-1', 'msg'=>'原数据有误']);
				
				$where2[] = ['auction_id', 'eq', $auction['id']];
				$where2[] = ['user_id', 'eq', $auction['curr_user_id']];
				$where2[] = ['is_bond', 'eq', 1];
				$auctionSignup = Db::table("gd_auction_signup") -> where($where2) -> find();
				if( empty($auctionSignup) ) returnResults(['code'=>'-1', 'msg'=>'参拍信息有误']);
				
				$save['receiving_address'] = $in['receiving_address'];
				$save['receiving_name'] = $in['receiving_name'];
				$save['receiving_phone'] = $in['receiving_phone'];
				
				$save = filterArr($save, array_keys($save), $auctionSignup);
				
				if( !empty($save) ){
					$re = Db::table("gd_auction_signup") -> where($where2) -> update($save);
					if( !$re ) returnResults2('-1', '编辑失败,请联系管理员');
				}
				
				$outArr['code'] = '1';
				
//				写入操作日志
				$old_data_json = $auctionSignup;
				controller("service/OperLog") -> logs(
					[
						'title'=>'拍卖保存收货信息',
						'business_type'=>3,
						'oper_name'=>$userInfo['info']['nickname'],
						'oper_user_id'=>$userInfo['info']['id'],
						'json'=>[
							[
								'json_result'=>$outArr,
								'old_data'=> isset($old_data_json) ? $old_data_json : [],
							]
						]
					]
				);
			}
		}else{
			$outArr['code'] = '50001';
		}
		returnResults($outArr);
	}
	
	/**
	 * 批量上下架
	 */
	public function batchEditStatus(){
		if(request() -> isPost()){
			$in = input("post.");
			$bool = is_empty(['user_id', 'id', 'status'], $in);
			if($bool){
				$outArr['code'] = '40004';
			}else{
				$userInfo = $this -> getUserInfo();
				
				$where[] = ['id', 'in', $in['id']];
				$oldData = Db::table("gd_auction") -> where($where) -> select();
				if( empty($oldData) ) returnResults(['code'=>'-1', 'msg'=>'原数据有误']);
				
				$date = date("Y-m-d H:i:s", time());
				foreach ($oldData as $k => $v) {
//					if( $v['start_date'] < $date && $v['end_date2'] > $date ) returnResults2('-1', '操作失败,正在拍卖的数据无法上下架操作');
					if( $v['verify_status'] != 1 && $in['status'] == 1 ) returnResults2('-1', '操作失败,只能上架已通过审核的数据');
					if( $v['status'] == $in['status'] ) unset($oldData[$k]);
				}
				
				if( !empty($oldData) ){
					$re = Db::table("gd_auction") -> where($where) -> update(['status'=>$in['status']]);
					if( !$re ) returnResults2('-1', '编辑失败,请联系管理员');
				}
				$outArr['code'] = '1';
				
//				写入操作日志
				$old_data_json = $oldData;
				controller("service/OperLog") -> logs(
					[
						'title'=>'拍卖批量上下架',
						'business_type'=>3,
						'oper_name'=>$userInfo['info']['nickname'],
						'oper_user_id'=>$userInfo['info']['id'],
						'json'=>[
							[
								'json_result'=>$outArr,
								'old_data'=> isset($old_data_json) ? $old_data_json : [],
							]
						]
					]
				);
			}
		}else{
			$outArr['code'] = '50001';
		}
		returnResults($outArr);
	}
	
	/**
	 * 获取拍卖保证金支付/退款记录
	 */
	public function getPaymentRecords(){
		if(request() -> isPost()){
			$in = input("post.");
			$bool = is_empty(['user_id', 'auction_id', 'searchUserId'], $in);
			if($bool){
				$outArr['code'] = '40004';
			}else{
				$userInfo = $this -> getUserInfo();
				
				$where[] = ['auction_id', 'eq', $in['auction_id']];
				$where[] = ['create_user_id', 'eq', $in['searchUserId']];
				$where[] = ['pay_status', 'eq', 1];
				$data = Db::table("gd_payment_records") -> where($where) -> find();
				if( empty($data) ) returnResults2('-1', '操作失败,拍卖数据有误');
				
				$outArr['code'] = '1';
				$outArr['info'] = $data;
				
//				写入操作日志
				controller("service/OperLog") -> logs(
					[
						'title'=>'获取拍卖保证金支付/退款记录',
						'business_type'=>4,
						'oper_name'=>$userInfo['info']['nickname'],
						'oper_user_id'=>$userInfo['info']['id'],
						'json'=>[
							[
								'json_result'=>$outArr,
								'old_data'=> isset($old_data_json) ? $old_data_json : [],
							]
						]
					]
				);
			}
		}else{
			$outArr['code'] = '50001';
		}
		returnResults($outArr);
	}
	
	/**
	 * 拍卖保证金退款
	 */
	public function refund(){
		if(request() -> isPost()){
			$in = input("post.");
			$bool = is_empty(['user_id', 'auction_id', 'searchUserId', 'refund_type', 'refund_file'], $in);
			if($bool){
				$outArr['code'] = '40004';
			}else{
				$userInfo = $this -> getUserInfo();
				
				$where[] = ['id', 'eq', $in['auction_id']];
				$where[] = ['is_delete', 'eq', 0];
				$where[] = ['verify_status', 'eq', 1];
				$oldData = Db::table("gd_auction") -> where($where) -> find();
				if( empty($oldData) ) returnResults2('-1', '操作失败,拍卖数据有误');
				
				Db::startTrans();
				
				$where2[] = ['auction_id', 'eq', $in['auction_id']];
				$where2[] = ['user_id', 'eq', $in['searchUserId']];
				$where2[] = ['status', 'neq', 0];
				$where2[] = ['is_bond', 'eq', 1];
				$oldData2 = Db::table("gd_auction_signup") -> where($where2) -> find();
				if( empty($oldData2) ) {
					Db::rollback();
					returnResults2('-1', '操作失败,拍卖报名数据有误');
				}
				if( $oldData2['status'] != 1 ) {
					Db::rollback();
					returnResults2('-1', '操作失败,拍卖报名数据状态有误');
				}
				
				$where3[] = ['auction_id', 'eq', $in['auction_id']];
				$where3[] = ['auction_signup_id', 'eq', $oldData2['id']];
				$oldData3 = Db::table("gd_payment_records") -> where($where3) -> find();
				if( empty($oldData3) ) {
					Db::rollback();
					returnResults2('-1', '操作失败,拍卖报名保证金数据有误');
				}
				if( $oldData3['pay_status'] != 1 ) {
					Db::rollback();
					returnResults2('-1', '操作失败,拍卖报名保证金支付状态有误');
				}
				if( $oldData3['refund_status'] != 0 ) {
					Db::rollback();
					returnResults2('-1', '操作失败,拍卖报名保证金退款状态有误');
				}
				
//				变更拍卖报名表
				$save2['status'] = 3;
				$re2 = Db::table("gd_auction_signup") -> where($where2) -> update($save2);
				if( !$re2 ) {
					Db::rollback();
					returnResults2('-1', '变更拍卖报名数据失败,请联系管理员');
				}
				
//				变更支付记录表
				$save3['refund_status'] = 1;
				$save3['refund_price'] = $oldData3['pay_price'];
				$save3['refund_desc'] = '后台操作退款';
				$save3['refund_create_date'] = date("Y-m-d H:i:s", time());
				$save3['refund_date'] = date("Y-m-d H:i:s", time());
				$save3['refund_type'] = $in['refund_type'];
				$save3['refund_file'] = is_array($in['refund_file']) ? json_encode($in['refund_file'], JSON_UNESCAPED_UNICODE) : $in['refund_file'];
				$re3 = Db::table("gd_payment_records") -> where($where3) -> update($save3);
				if( !$re3 ) {
					Db::rollback();
					returnResults2('-1', '变更拍卖报名保证金支付数据失败,请联系管理员');
				}
				
				Db::commit();
				
				$outArr['code'] = '1';
				
//				写入操作日志
				controller("service/OperLog") -> logs(
					[
						'title'=>'拍卖保证金退款',
						'business_type'=>4,
						'oper_name'=>$userInfo['info']['nickname'],
						'oper_user_id'=>$userInfo['info']['id'],
						'json'=>[
							[
								'json_result'=>$outArr,
								'old_data'=> isset($old_data_json) ? $old_data_json : [],
							]
						]
					]
				);
			}
		}else{
			$outArr['code'] = '50001';
		}
		returnResults($outArr);
	}
	
	/**
	 * 获取拍卖保证金详情
	 */
	public function getAuctionDetails(){
		if(request() -> isPost()){
			$in = input("post.");
			$bool = is_empty(['user_id', 'searchUserId', 'auction_id'], $in);
			if($bool){
				$outArr['code'] = '40004';
			}else{
				$userInfo = $this -> getUserInfo();
				
				$where[] = ['a.id', 'eq', $in['auction_id']];
				$where[] = ['a.is_delete', 'eq', 0];
				$where[] = ['a.verify_status', 'eq', 1];
				$where[] = ['b.is_bond', 'eq', 1];
				$where[] = ['b.user_id', 'eq', $in['searchUserId']];
				$data = Db::table("gd_auction") -> alias('a')
												-> join('gd_auction_signup b', 'a.id = b.auction_id and a.curr_user_id != b.user_id')
												-> field(['a.*', 'b.id'=>'auction_signup_id', 'b.earnest_money'=>'user_earnest_money', 'b.create_user_name'=>'auctionsignupUserName', 'b.create_user_id'=>'auctionsignupUserId', 'b.status'=>'refundStatus', 'b.id'=>'auction_signup_id'])
								                -> where($where)
												-> find();
				
				if( !empty($data) ){
					$data['auctionsignupUserPhone'] = Db::table("gd_user") -> where(['id'=>$data['auctionsignupUserId']]) -> find()['phone'];
					$data['refundDate'] = Db::table("gd_payment_records") -> where(['auction_signup_id'=>$data['auction_signup_id']]) -> find()['refund_date'];
					if( $data['start_date'] <= date("Y-m-d H:i:s", time()) && $data['end_date'] >= date("Y-m-d H:i:s", time()) ){
						$data['isInProgress'] = '进行中';
					}
					if( $data['start_date'] > date("Y-m-d H:i:s", time()) ){
						$data['isInProgress'] = '未开始';
					}
					if( $data['end_date'] < date("Y-m-d H:i:s", time()) ){
						$data['isInProgress'] = '已结束';
					}
				}
				
				$outArr['code'] = '1';
				$outArr['info'] = $data;
				$outArr['totalNums'] = $count;				
				
//				写入操作日志
				controller("service/OperLog") -> logs(
					[
						'title'=>'获取拍卖保证金详情',
						'business_type'=>4,
						'oper_name'=>$userInfo['info']['nickname'],
						'oper_user_id'=>$userInfo['info']['id'],
						'json'=>[
							[
								'json_result'=>$outArr,
								'old_data'=> isset($old_data_json) ? $old_data_json : [],
							]
						]
					]
				);
			}
		}else{
			$outArr['code'] = '50001';
		}
		returnResults($outArr);
	}
	
	/**
	 * 获取拍卖保证金列表
	 */
	public function getAuctionList(){
		if(request() -> isPost()){
			$in = input("post.");
			$bool = is_empty(['user_id'], $in);
			if($bool){
				$outArr['code'] = '40004';
			}else{
				$userInfo = $this -> getUserInfo();
				
				$num = isset($in['num']) ? $in['num'] : 20;
				$start = isset($in['page']) && $in['page'] > 0 ? (($in['page'] - 1) * $num) : 0;
				
				$where[] = ['a.is_delete', 'eq', 0];
				$where[] = ['a.verify_status', 'eq', 1];
				$where[] = ['b.is_bond', 'eq', 1];
				if( isset($in['isInRefund']) && $in['isInRefund'] == 0 ) {
//					待退
					$where[] = ['b.status', 'eq', 1];
					$where[] = ['a.end_date2', 'lt', date("Y-m-d H:i:s", time())];
				}
				if( isset($in['isInRefund']) && $in['isInRefund'] == 1 ) {
//					已退
					$where[] = ['b.status', 'eq', 3];
				}
				
				if( isset($in['searchGoodsName']) ) $where[] = ['a.name', 'like', '%'.trim($in['searchGoodsName']).'%'];
				if( isset($in['searchAuctionDate']) ) {
					$where[] = ['a.start_date', 'lt', $in['searchAuctionDate'][0]];
					$where[] = ['a.end_date2', 'gt', $in['searchAuctionDate'][1]];
				}
				$data = Db::table("gd_auction") -> alias('a')
												-> join('gd_auction_signup b', 'a.id = b.auction_id and a.curr_user_id != b.user_id')
												-> field(['a.*', 'b.id'=>'auction_signup_id', 'b.earnest_money'=>'user_earnest_money', 'b.create_user_name'=>'auctionsignupUserName', 'b.create_user_id'=>'auctionsignupUserId', 'b.status'=>'refundStatus', 'b.id'=>'auction_signup_id'])
								                -> where($where)
												-> limit($start, $num) 
												-> select();
				$count = Db::table("gd_auction") -> alias('a')
												-> join('gd_auction_signup b', 'a.id = b.auction_id and a.curr_user_id != b.user_id')
								                -> where($where)
												-> count();
				
				if( !empty($data) ){
					foreach ($data as $k => $v) {
						$data[$k]['auctionsignupUserPhone'] = Db::table("gd_user") -> where(['id'=>$v['auctionsignupUserId']]) -> find()['phone'];
						$data[$k]['refundDate'] = Db::table("gd_payment_records") -> where(['auction_signup_id'=>$v['auction_signup_id']]) -> find()['refund_date'];
						if( $v['start_date'] <= date("Y-m-d H:i:s", time()) && $v['end_date'] >= date("Y-m-d H:i:s", time()) ){
							$data[$k]['isInProgress'] = '进行中';
						}
						if( $v['start_date'] > date("Y-m-d H:i:s", time()) ){
							$data[$k]['isInProgress'] = '未开始';
						}
						if( $v['end_date'] < date("Y-m-d H:i:s", time()) ){
							$data[$k]['isInProgress'] = '已结束';
						}
						if( $v['curr_user_id'] == $v['auctionsignupUserId'] ){
							$data[$k]['isInRefund'] = '已中标';
						}
						if( $v['curr_user_id'] != $v['auctionsignupUserId'] && $v['refundStatus'] == 3 ){
							$data[$k]['isInRefund'] = '已退款';
						}
						if( $v['curr_user_id'] != $v['auctionsignupUserId'] && $v['refundStatus'] == 1 ){
							$data[$k]['isInRefund'] = '待退款';
						}
					}
				}
				
				$outArr['code'] = '1';
				$outArr['info'] = $data;
				$outArr['totalNums'] = $count;				
				
//				写入操作日志
				controller("service/OperLog") -> logs(
					[
						'title'=>'获取拍卖保证金列表',
						'business_type'=>4,
						'oper_name'=>$userInfo['info']['nickname'],
						'oper_user_id'=>$userInfo['info']['id'],
						'json'=>[
							[
								'json_result'=>$outArr,
								'old_data'=> isset($old_data_json) ? $old_data_json : [],
							]
						]
					]
				);
			}
		}else{
			$outArr['code'] = '50001';
		}
		returnResults($outArr);
	}

	/**
	 * 获取拍卖保证金列表(所有已交保证金)
	 */
	public function getAuctionAllList(){
		if(request() -> isPost()){
			$in = input("post.");
			$bool = is_empty(['user_id'], $in);
			if($bool){
				$outArr['code'] = '40004';
			}else{
				$userInfo = $this -> getUserInfo();
				
				$num = isset($in['num']) ? $in['num'] : 20;
				$start = isset($in['page']) && $in['page'] > 0 ? (($in['page'] - 1) * $num) : 0;
				
				$where[] = ['a.is_delete', 'eq', 0];
				$where[] = ['a.verify_status', 'eq', 1];
				$where[] = ['b.is_bond', 'eq', 1];
				
				if( isset($in['searchGoodsName']) ) $where[] = ['a.name', 'like', '%'.trim($in['searchGoodsName']).'%'];
				if( isset($in['searchAuctionDate']) ) {
					$where[] = ['a.start_date', 'lt', $in['searchAuctionDate'][0]];
					$where[] = ['a.end_date2', 'gt', $in['searchAuctionDate'][1]];
				}
				$data = Db::table("gd_auction") -> alias('a')
												-> join('gd_auction_signup b', 'a.id = b.auction_id')
												-> field(['a.*', 'b.id'=>'auction_signup_id', 'b.earnest_money'=>'user_earnest_money', 'b.create_user_name'=>'auctionsignupUserName', 'b.create_user_id'=>'auctionsignupUserId', 'b.status'=>'refundStatus', 'b.id'=>'auction_signup_id'])
								                -> where($where)
												-> limit($start, $num) 
												-> select();
				$count = Db::table("gd_auction") -> alias('a')
												-> join('gd_auction_signup b', 'a.id = b.auction_id')
								                -> where($where)
												-> count();
				
				if( !empty($data) ){
					foreach ($data as $k => $v) {
						$data[$k]['auctionsignupUserPhone'] = Db::table("gd_user") -> where(['id'=>$v['auctionsignupUserId']]) -> find()['phone'];
						$data[$k]['refundDate'] = Db::table("gd_payment_records") -> where(['auction_signup_id'=>$v['auction_signup_id']]) -> find()['refund_date'];
						if( $v['start_date'] <= date("Y-m-d H:i:s", time()) && $v['end_date'] >= date("Y-m-d H:i:s", time()) ){
							$data[$k]['isInProgress'] = '进行中';
						}
						if( $v['start_date'] > date("Y-m-d H:i:s", time()) ){
							$data[$k]['isInProgress'] = '未开始';
						}
						if( $v['end_date'] < date("Y-m-d H:i:s", time()) ){
							$data[$k]['isInProgress'] = '已结束';
						}
						if( $v['curr_user_id'] == $v['auctionsignupUserId'] ){
							$data[$k]['isInRefund'] = '已中标';
						}
						if( $v['curr_user_id'] != $v['auctionsignupUserId'] && $v['refundStatus'] == 3 ){
							$data[$k]['isInRefund'] = '已退款';
						}
						if( $v['curr_user_id'] != $v['auctionsignupUserId'] && $v['refundStatus'] == 1 ){
							$data[$k]['isInRefund'] = '待退款';
						}
					}
				}
				
				$outArr['code'] = '1';
				$outArr['info'] = $data;
				$outArr['totalNums'] = $count;				
				
//				写入操作日志
				controller("service/OperLog") -> logs(
					[
						'title'=>'获取拍卖保证金列表(所有已交保证金)',
						'business_type'=>4,
						'oper_name'=>$userInfo['info']['nickname'],
						'oper_user_id'=>$userInfo['info']['id'],
						'json'=>[
							[
								'json_result'=>$outArr,
								'old_data'=> isset($old_data_json) ? $old_data_json : [],
							]
						]
					]
				);
			}
		}else{
			$outArr['code'] = '50001';
		}
		returnResults($outArr);
	}
	
	/**
	 * 拍卖发货
	 */
	public function delivery(){
		if(request() -> isPost()){
			$in = input("post.");
			$bool = is_empty(['user_id', 'id', 'waybill_name', 'waybill_code'], $in);
			if($bool){
				$outArr['code'] = '40004';
			}else{
				$userInfo = $this -> getUserInfo();
				
				$where[] = ['id', 'eq', $in['id']];
				$oldData = Db::table("gd_auction") -> where($where) -> find();
				if( !empty($oldData['waybill_code']) ) returnResults2('-1', '操作失败,不能重复发货');
				
				$save['waybill_name'] = $in['waybill_name'];
				$save['waybill_code'] = $in['waybill_code'];
				$save['delivery_status'] = 1;
				$save['waybill_date'] = date("Y-m-d H:i:s", time());
				$save['verify_notes'] = isset($in['verify_notes']) ? $in['verify_notes'] : $oldData['verify_notes'];
				$save['waybill_notes'] = isset($in['waybill_notes']) ? $in['waybill_notes'] : $oldData['waybill_notes'];
				
				$re = Db::table("gd_auction") -> where($where) -> update($save);
				if( !$re ) {
					Db::rollback();
					returnResults(['code'=>'-1', 'msg'=>'操作失败,请联系管理员']);
				}
				
				$outArr['code'] = '1';
				
//				写入操作日志
				$old_data_json = $oldData;
				controller("service/OperLog") -> logs(
					[
						'title'=>'拍卖发货',
						'business_type'=>3,
						'oper_name'=>$userInfo['info']['nickname'],
						'oper_user_id'=>$userInfo['info']['id'],
						'json'=>[
							[
								'json_result'=>$outArr,
								'old_data'=> isset($old_data_json) ? $old_data_json : [],
							]
						]
					]
				);
			}
		}else{
			$outArr['code'] = '50001';
		}
		returnResults($outArr);
	}
	
	/**
	 * 拍卖出价
	 */
	public function bidPrice(){
		if(request() -> isPost()){
			$in = input("post.");
			$bool = is_empty(['user_id', 'id', 'price'], $in);
			if($bool){
				$outArr['code'] = '40004';
			}else{
				$userInfo = $this -> getUserInfo();
				
				Db::startTrans();
				
				$where[] = ['id', 'eq', $in['id']];
				$oldData = Db::table("gd_auction") -> where($where) -> lock(true) -> find();
				if( empty($oldData) ) {
					Db::rollback();
					returnResults2('-1', '操作失败,原数据有误');
				}
				if( $oldData['start_date'] > date("Y-m-d H:i:s", time()) ) {
					Db::rollback();
					returnResults2('-1', '操作失败,该拍卖未开始');
				}
				if( $oldData['end_date2'] < date("Y-m-d H:i:s", time()) ) {
					Db::rollback();
					returnResults2('-1', '操作失败,该拍卖已结束');
				}
				
				$auctionSignupWhere[] = ['auction_id', 'eq', $oldData['id']];
				$auctionSignupWhere[] = ['user_id', 'eq', $in['user_id']];
				$auctionSignupWhere[] = ['status', 'eq', 1];
				$oldData2 = Db::table("gd_auction_signup") -> where($auctionSignupWhere) -> find();
				if( empty($oldData2) ) {
					Db::rollback();
					returnResults2('-1', '操作失败,该拍卖未报名成功');
				}
				
				if( $in['price'] < $oldData['curr_price'] || $in['price'] == $oldData['curr_price'] ){
					Db::rollback();
					returnResults2('-1', '操作失败,出价金额不能小于已有报价');
				}
				
				if( time() + 300 > strtotime($oldData['end_date2']) ) $save['end_date2'] = date("Y-m-d H:i:s", strtotime($oldData['end_date2']) + 300);
				$save['curr_nums'] = $oldData['curr_nums'] +1;
				$save['curr_price'] = $in['price'];
				$save['curr_user_id'] = $userInfo['info']['id'];
				$save['curr_user_name'] = $userInfo['info']['nickname'];
				$save['curr_version'] = $oldData['curr_version'] +1;
				$save['curr_date'] = date("Y-m-d H:i:s", time());
				
				$where[] = ['curr_version', 'eq', $oldData['curr_version']];
				$re = Db::table("gd_auction") -> where($where) -> update($save);
				if( !$re ) {
					Db::rollback();
					returnResults2('-1', '出价失败,当前出价已失效,请重新出价');
				}
				
				$newData['auction_id'] = $oldData['id'];
				$newData['auction_signup_id'] = $oldData2['id'];
				$newData['price'] = $in['price'];
				$newData['up_price'] = $in['price'] - $oldData['curr_price'];
				$newData['create_user_id'] = $userInfo['info']['id'];
				$newData['create_user_name'] = $userInfo['info']['nickname'];
				$newData['create_user_type'] = $userInfo['info']['type'];
				$newData['create_date'] = date("Y-m-d H:i:s", time());
				
				$re = Db::table("gd_auction_bidprice") -> insertGetId($newData);
				if( !$re ) {
					Db::rollback();
					returnResults(['code'=>'-1', 'msg'=>'生成出价记录失败,请重试']);
				}
				$oldData = Db::name("gd_auction") -> where($where) -> lock(false) -> find();
				Db::commit();
				
				$outArr['code'] = '1';
				
//				写入操作日志
				$old_data_json = $oldData;
				controller("service/OperLog") -> logs(
					[
						'title'=>'拍卖出价',
						'business_type'=>1,
						'oper_name'=>$userInfo['info']['nickname'],
						'oper_user_id'=>$userInfo['info']['id'],
						'json'=>[
							[
								'json_result'=>$outArr,
								'old_data'=> isset($old_data_json) ? $old_data_json : [],
							]
						]
					]
				);
			}
		}else{
			$outArr['code'] = '50001';
		}
		returnResults($outArr);
	}
	
	/**
	 * 拍卖报名
	 */
	public function signUp(){
		if(request() -> isPost()){
			$in = input("post.");
			$bool = is_empty(['user_id', 'id'], $in);
			if($bool){
				$outArr['code'] = '40004';
			}else{
				$userInfo = $this -> getUserInfo();
				
				$where[] = ['id', 'eq', $in['id']];
				$oldData = Db::table("gd_auction") -> where($where) -> find();
				if( empty($oldData) ) returnResults2('-1', '操作失败,原数据有误');
				if( $oldData['end_date2'] < date("Y-m-d H:i:s", time()) ) returnResults2('-1', '操作失败,该拍卖已结束');
				
				$auctionSignupWhere[] = ['auction_id', 'eq', $oldData['id']];
				$auctionSignupWhere[] = ['user_id', 'eq', $in['user_id']];
				$auctionSignupWhere[] = ['status', 'eq', 1];
				$oldData2 = Db::table("gd_auction_signup") -> where($auctionSignupWhere) -> find();
				if( !empty($oldData2) ) returnResults2('-1', '操作失败,已报名当前拍品');
				
				$newData['auction_id'] = $oldData['id'];
				$newData['earnest_money'] = $oldData['earnest_money'];
				$newData['user_id'] = $in['user_id'];
				$newData['pay_date'] = '0000-00-00 00:00:00';
				$newData['create_user_id'] = $userInfo['info']['id'];
				$newData['create_user_name'] = $userInfo['info']['nickname'];
				$newData['create_user_type'] = $userInfo['info']['type'];
				$newData['create_date'] = date("Y-m-d H:i:s", time());
				
				Db::startTrans();
				
				$newAuctionSignupId = Db::table("gd_auction_signup") -> insertGetId($newData);
				if( !$newAuctionSignupId ) {
					Db::rollback();
					returnResults(['code'=>'-1', 'msg'=>'操作失败,请联系管理员']);
				}
				
//				统一下单
				$goodsName = $oldData['name'];
				$totalPrice = $oldData['earnest_money'] * 100;
				$openId = $userInfo['info']['open_id'];
				$attach = ['payType'=>3, 'auctionId'=>$oldData['id'], 'auctionSignupId'=>$newAuctionSignupId];
				$re = controller("service/Wechat") -> unifiedOrder($goodsName, $totalPrice, $openId, $attach, $userInfo['info']);
				if( $re['code'] != '1' ){
					Db::rollback();
					returnResults(['code'=>'-1', 'msg'=>'统一下单操作失败,请联系管理员', 'info'=>$re]);
				}
				
				Db::commit();
				
				$outArr['code'] = '1';
				$outArr['info'] = $re['info'];
				
//				写入操作日志
				$old_data_json = $oldData;
				controller("service/OperLog") -> logs(
					[
						'title'=>'拍卖报名',
						'business_type'=>1,
						'oper_name'=>$userInfo['info']['nickname'],
						'oper_user_id'=>$userInfo['info']['id'],
						'json'=>[
							[
								'json_result'=>$outArr,
								'old_data'=> isset($old_data_json) ? $old_data_json : [],
							]
						]
					]
				);
			}
		}else{
			$outArr['code'] = '50001';
		}
		returnResults($outArr);
	}

	/**
	 * 微信统一下单（待支付的拍卖报名保证金）
	 */
	public function unifiedOrderState3(){
		if(request() -> isPost()){
			$in = input("post.");
			$bool = is_empty(['user_id', 'auction_signup_id'], $in);
			if($bool){
				$outArr['code'] = '40004';
			}else{
				$userInfo = $this -> getUserInfo();
				
//				获取订单
				$where[] = ['id', 'eq', $in['auction_signup_id']];
				$oldData = Db::table("gd_auction_signup") -> where($where) -> find();
				if( empty($oldData) ) returnResults2('-1', '操作失败,查询拍卖报名数据有误,请联系管理员');
				if( $oldData['status'] != 0 ) returnResults2('-1', '操作失败,仅支持待支付的拍卖报名,请联系管理员');
				
				$auction = Db::table("gd_auction") -> where(['id'=>$oldData['auction_id']]) -> find();
				if( $auction['end_date'] < date("Y-m-d H:i:s", time()) ) returnResults2('-1', '操作失败,该拍卖已经结束,请联系管理员');
				
//				统一下单
				$goodsName = $auction['name'];
				$totalPrice = $oldData['earnest_money'] * 100;
				$openId = $userInfo['info']['open_id'];
				$attach = ['payType'=>3, 'auctionId'=>$auction['id'], 'auctionSignupId'=>$oldData['id']];
				$re = controller("service/Wechat") -> unifiedOrder($goodsName, $totalPrice, $openId, $attach, $userInfo['info']);
				if( $re['code'] != '1' ){
					returnResults2('-1', '统一下单操作失败,请联系管理员');
				}
				
				$outArr['code'] = '1';
				$outArr['info'] = $re['info'];
				
//				写入操作日志
				$old_data_json = $order;
				controller("service/OperLog") -> logs(
					[
						'title'=>'微信统一下单（待支付的拍卖报名保证金）',
						'business_type'=>3,
						'oper_name'=>$userInfo['info']['nickname'],
						'oper_user_id'=>$userInfo['info']['id'],
						'json'=>[
							[
								'json_result'=>$outArr,
								'old_data'=> isset($old_data_json) ? $old_data_json : [],
							]
						]
					]
				);
			}
		}else{
			$outArr['code'] = '50001';
		}
		returnResults($outArr);
	}

	/**
	 * 微信统一下单（待支付的拍卖尾款）
	 */
	public function unifiedOrderState4(){
		if(request() -> isPost()){
			$in = input("post.");
			$bool = is_empty(['user_id', 'auction_id'], $in);
			if($bool){
				$outArr['code'] = '40004';
			}else{
				$userInfo = $this -> getUserInfo();
				
//				获取订单
				$auctionWhere[] = ['id', 'eq', $in['auction_id']];
				$auction = Db::table("gd_auction") -> where($auctionWhere) -> find();
				if( empty($auction) ) returnResults2('-1', '操作失败,查询拍卖数据有误,请联系管理员');
				if( $auction['end_date'] > date("Y-m-d H:i:s", time()) ) returnResults2('-1', '操作失败,该拍卖未结束,请联系管理员');
				if( $auction['curr_user_id'] != $userInfo['info']['id'] ) returnResults2('-1', '操作失败,当前登录账号不是中拍人账号,请联系管理员');
				
				$auctionSignupWhere[] = ['auction_id', 'eq', $auction['id']];
				$auctionSignupWhere[] = ['user_id', 'eq', $userInfo['info']['id']];
				$auctionSignup = Db::table("gd_auction_signup") -> where($auctionSignupWhere) -> find();
				if( empty($auctionSignup) ) returnResults2('-1', '操作失败,查询拍卖报名数据有误,请联系管理员');
				
//				统一下单
				$goodsName = $auction['name'];
				$totalPrice = ($auction['curr_price'] - $auctionSignup['earnest_money']) * 100;
				$openId = $userInfo['info']['open_id'];
				$attach = ['payType'=>4, 'auctionId'=>$auction['id'], 'auctionSignupId'=>$auctionSignup['id']];
				if( $totalPrice == 0 ) returnResults2('-1', '金额有误,请联系管理员');
				
				$re = controller("service/Wechat") -> unifiedOrder($goodsName, $totalPrice, $openId, $attach, $userInfo['info']);
				if( $re['code'] != '1' ){
					returnResults(['code'=>'-1', 'msg'=>'统一下单操作失败,请联系管理员', 'info'=>$re]);
//					returnResults2('-1', '统一下单操作失败,请联系管理员', $re);
				}
				
				$outArr['code'] = '1';
				$outArr['info'] = $re['info'];
				
//				写入操作日志
				$old_data_json = $auction;
				controller("service/OperLog") -> logs(
					[
						'title'=>'微信统一下单（待支付的拍卖报名保证金）',
						'business_type'=>3,
						'oper_name'=>$userInfo['info']['nickname'],
						'oper_user_id'=>$userInfo['info']['id'],
						'json'=>[
							[
								'json_result'=>$outArr,
								'old_data'=> isset($old_data_json) ? $old_data_json : [],
							]
						]
					]
				);
			}
		}else{
			$outArr['code'] = '50001';
		}
		returnResults($outArr);
	}
	
	/**
	 * 审核
	 */
	public function verify(){
		if(request() -> isPost()){
			$in = input("post.");
			$bool = is_empty(['user_id', 'id', 'verify_status'], $in);
			if($bool){
				$outArr['code'] = '40004';
			}else{
				$userInfo = $this -> getUserInfo();
				
				$where[] = ['id', 'eq', $in['id']];
				$oldData = Db::table("gd_auction") -> where($where) -> find();
				
				$save['verify_status'] = $in['verify_status'];
				$save['verify_notes'] = isset($in['verify_notes']) ? $in['verify_notes'] : $oldData['verify_notes'];
				
				$re = Db::table("gd_auction") -> where($where) -> update($save);
				if( !$re ) {
					Db::rollback();
					returnResults(['code'=>'-1', 'msg'=>'操作失败,请联系管理员']);
				}
				
				$outArr['code'] = '1';
				
//				写入操作日志
				$old_data_json = $oldData;
				controller("service/OperLog") -> logs(
					[
						'title'=>'审核拍卖信息',
						'business_type'=>3,
						'oper_name'=>$userInfo['info']['nickname'],
						'oper_user_id'=>$userInfo['info']['id'],
						'json'=>[
							[
								'json_result'=>$outArr,
								'old_data'=> isset($old_data_json) ? $old_data_json : [],
							]
						]
					]
				);
			}
		}else{
			$outArr['code'] = '50001';
		}
		returnResults($outArr);
	}
	
	/**
	 * 批量删除
	 */
	public function batchDelete(){
		if(request() -> isPost()){
			$in = input("post.");
			$bool = is_empty(['user_id', 'id'], $in);
			if($bool){
				$outArr['code'] = '40004';
			}else{
				$userInfo = $this -> getUserInfo();
				
				$where[] = ['id', 'in', $in['id']];
				$oldData = Db::table("gd_auction") -> where($where) -> select();
				
				if( !empty($oldData) ){
					Db::startTrans();
					foreach ($oldData as $k => $v) {
						if( $v['is_delete'] != 1 ){
							$save['is_delete'] = 1;
							$delRes = Db::table("gd_auction") -> where(['id'=>$v['id']]) -> update($save);
							if( !$delRes ) {
								Db::rollback();
								returnResults(['code'=>'-1', 'msg'=>'操作失败,请联系管理员']);
							}
						}
					}
					Db::commit();
				}
				
				$outArr['code'] = '1';
				
//				写入操作日志
				$old_data_json = $oldData;
				controller("service/OperLog") -> logs(
					[
						'title'=>'批量删除拍卖',
						'business_type'=>2,
						'oper_name'=>$userInfo['info']['nickname'],
						'oper_user_id'=>$userInfo['info']['id'],
						'json'=>[
							[
								'json_result'=>$outArr,
								'old_data'=> isset($old_data_json) ? $old_data_json : [],
							]
						]
					]
				);
			}
		}else{
			$outArr['code'] = '50001';
		}
		returnResults($outArr);
	}

	/**
	 * 获取用户拍卖详情
	 */
	public function getUserAuctionDetails(){
		if(request() -> isPost()){
			$in = input("post.");
			$bool = is_empty(['user_id', 'auction_signup_id'], $in);
			if($bool){
				$outArr['code'] = '40004';
			}else{
				$userInfo = $this -> getUserInfo();
				
				$where[] = ['id', 'eq', $in['auction_signup_id']];
				
				$data = Db::table("gd_auction_signup") -> where($where) -> find();
				
				if( !empty($data) ){
					$data['auction'] = Db::table("gd_auction_") -> where(['id'=>$data['auction_id']]) -> find();
				}
				
				$outArr['code'] = '1';
				$outArr['info'] = $data;
				
//				写入操作日志
				controller("service/OperLog") -> logs(
					[
						'title'=>'获取用户拍卖详情',
						'business_type'=>4,
						'oper_name'=>$userInfo['info']['nickname'],
						'oper_user_id'=>$userInfo['info']['id'],
						'json'=>[
							[
								'json_result'=>$outArr,
								'old_data'=> isset($old_data_json) ? $old_data_json : [],
							]
						]
					]
				);
			}
		}else{
			$outArr['code'] = '50001';
		}
		returnResults($outArr);
	}

	/**
	 * 获取用户拍卖列表
	 */
	public function getUserAuctionList(){
		if(request() -> isPost()){
			$in = input("post.");
			$bool = is_empty(['user_id'], $in);
			if($bool){
				$outArr['code'] = '40004';
			}else{
				$userInfo = $this -> getUserInfo();
				
				$num = isset($in['num']) ? $in['num'] : 20;
				$start = isset($in['page']) && $in['page'] > 0 ? (($in['page'] - 1) * $num) : 0;
				
				$where[] = ['a.is_delete', 'eq', 0];
				$where[] = ['a.verify_status', 'eq', 1];
				$where[] = ['b.user_id', 'eq', $in['user_id']];
				if( isset($in['type']) && $in['type'] == '1' ){
					$where[] = ['b.status', 'eq', 1];
//					参拍中
//					$where[] = ['a.start_date', 'lt', date("Y-m-d H:i:s", time())];
//					$where[] = ['a.end_date2', 'gt', date("Y-m-d H:i:s", time())];
				}
				if( isset($in['type']) && $in['type'] == '2' ){
//					已获拍
					$where[] = ['b.status', 'eq', 1];
					$where[] = ['a.end_date2', 'lt', date("Y-m-d H:i:s", time())];
					$where[] = ['a.curr_user_id', 'eq', $in['user_id']];
				}
				if( isset($in['type']) && $in['type'] == '3' ){
//					未获拍
					$where[] = ['b.status', 'gt', 0];
					$where[] = ['a.end_date2', 'lt', date("Y-m-d H:i:s", time())];
					$where[] = ['a.curr_user_id', 'neq', $in['user_id']];
				}
				
				$data = Db::table("gd_auction") -> alias('a')
												-> join('gd_auction_signup b', 'a.id = b.auction_id')
												-> field(['a.*', 'b.id'=>'auction_signup_id', 'b.earnest_money'=>'user_earnest_money', 'b.status'=>'user_auction_signup_status'])
								                -> where($where)
												-> limit($start, $num) 
												-> select();
				$count = Db::table("gd_auction") -> alias('a')
												-> join('gd_auction_signup b', 'a.id = b.auction_id', 'left')
								                -> where($where)
												-> count();
				
				if( !empty($data) ){
					foreach ($data as $k => $v) {
						$data[$k]['userBalancePayment'] = $v['curr_price'] - $v['user_earnest_money'];
						$data[$k]['createUserPhone'] = Db::table("gd_user") -> where(['id'=>$v['create_user_id']]) -> find()['phone'];
						$data[$k]['bidpriceNums'] = Db::table("gd_auction_bidprice") -> where(['auction_id'=>$v['id']]) -> count();
						if( !empty($v['class_id1']) ) $data[$k]['calssName'] = Db::table("gd_classification") -> where(['id'=>$v['class_id1']]) -> find()['name'];
						if( !empty($v['class_id2']) ) $data[$k]['calssName'] = Db::table("gd_classification") -> where(['id'=>$v['class_id2']]) -> find()['name'];
						if( !empty($v['class_id3']) ) $data[$k]['calssName'] = Db::table("gd_classification") -> where(['id'=>$v['class_id3']]) -> find()['name'];
						if( $v['start_date'] <= date("Y-m-d H:i:s", time()) && $v['end_date2'] >= date("Y-m-d H:i:s", time()) ){
							$data[$k]['isInProgress'] = '拍卖中';
							$data[$k]['type'] = '1';
						}
						if( $v['start_date'] > date("Y-m-d H:i:s", time()) ){
							$data[$k]['isInProgress'] = '预展中';
							$data[$k]['type'] = '1';
						}
						if( $v['end_date2'] < date("Y-m-d H:i:s", time()) && $v['curr_user_id'] != $in['user_id'] ){
							$data[$k]['isInProgress'] = '竞拍结束';
							$data[$k]['type'] = '3';
						}
						if( $v['end_date2'] < date("Y-m-d H:i:s", time()) && $v['curr_user_id'] == $in['user_id'] ){
							$data[$k]['isInProgress'] = '竞拍成功';
							$data[$k]['type'] = '2';
						}
					}
				}
				
				$outArr['code'] = '1';
				$outArr['info'] = $data;
				$outArr['totalNums'] = $count;				
				
//				写入操作日志
				controller("service/OperLog") -> logs(
					[
						'title'=>'获取用户拍卖列表',
						'business_type'=>4,
						'oper_name'=>$userInfo['info']['nickname'],
						'oper_user_id'=>$userInfo['info']['id'],
						'json'=>[
							[
								'json_result'=>$outArr,
								'old_data'=> isset($old_data_json) ? $old_data_json : [],
							]
						]
					]
				);
			}
		}else{
			$outArr['code'] = '50001';
		}
		returnResults($outArr);
	}

	/**
	 * 详情
	 */
	public function details(){
		if(request() -> isPost()){
			$in = input("post.");
			$bool = is_empty(['user_id', 'id'], $in);
			if($bool){
				$outArr['code'] = '40004';
			}else{
				$userInfo = $this -> getUserInfo();
				
				
				$where[] = ['id', 'eq', $in['id']];
				
				$data = Db::table("gd_auction") -> where($where) -> find();
				
				if( !empty($data) ){
					$isSignup = Db::table("gd_auction_signup") -> where(['auction_id'=>$data['id'], 'create_user_id'=>$in['user_id'], 'status'=>1]) -> find();
					$data['isSignup'] = empty($isSignup) ? FALSE : TRUE;
					$data['signupNums'] = Db::table("gd_auction_signup") -> where(['auction_id'=>$data['id'], 'status'=>1]) -> count();
					$data['auction_bidprice'] = Db::table("gd_auction_bidprice")-> alias('a')
																				-> join('gd_user b', 'a.create_user_id = b.id')
																				-> field(['a.*', 'b.nickname2', 'b.avatar'])
																                -> where(['a.auction_id'=>$data['id']])
																				-> group("a.id desc") 
																				-> select();
					$data['preach'] = Db::table("gd_preach") -> where(['auction_id'=>$data['id'], 'is_delete'=>0]) -> select();
					
					if( $data['start_date'] <= date("Y-m-d H:i:s", time()) && $data['end_date2'] >= date("Y-m-d H:i:s", time()) ){
						$data['isInProgress'] = '进行中';
					}
					if( $data['start_date'] > date("Y-m-d H:i:s", time()) ){
						$data['isInProgress'] = '未开始';
					}
					if( $data['end_date2'] < date("Y-m-d H:i:s", time()) ){
						$data['isInProgress'] = '已结束';
					}
				}
				
				$outArr['code'] = '1';
				$outArr['info'] = $data;
				
//				写入操作日志
				controller("service/OperLog") -> logs(
					[
						'title'=>'获取拍卖详情',
						'business_type'=>4,
						'oper_name'=>$userInfo['info']['nickname'],
						'oper_user_id'=>$userInfo['info']['id'],
						'json'=>[
							[
								'json_result'=>$outArr,
								'old_data'=> isset($old_data_json) ? $old_data_json : [],
							]
						]
					]
				);
			}
		}else{
			$outArr['code'] = '50001';
		}
		returnResults($outArr);
	}
	
	/**
	 * 列表
	 */
	public function index(){
		if(request() -> isPost()){
			$in = input("post.");
			$bool = is_empty(['user_id'], $in);
			if($bool){
				$outArr['code'] = '40004';
			}else{
				$userInfo = $this -> getUserInfo();
				
				$num = isset($in['num']) ? $in['num'] : 20;
				$start = isset($in['page']) && $in['page'] > 0 ? (($in['page'] - 1) * $num) : 0;
				
				$where[] = ['is_delete', 'eq', 0];
				if( isset($in['name']) ) $where[] = ['name', 'like', '%'.$in['name'].'%'];
				if( isset($in['verify_status']) ) $where[] = ['verify_status', 'eq', $in['verify_status']];
				if( isset($in['start_date']) && isset($in['end_date']) ) {
					$where[] = ['start_date', 'egt', $in['start_date']];
					$where[] = ['end_date', 'elt', $in['end_date']];
				}
				if( isset($in['isInProgress']) ){
					if( $in['isInProgress'] == 1 ){
//						进行中
						$where[] = ['start_date', 'elt', date("Y-m-d H:i:s", time())];
						$where[] = ['end_date2', 'egt', date("Y-m-d H:i:s", time())];
					}else{
						$where[] = ['end_date2', 'elt', date("Y-m-d H:i:s", time())];
					}
				}
				if( isset($in['isDeliveryStatus']) && $in['isDeliveryStatus'] == 1 ){
//					已发货
					$where[] = ['delivery_status', 'eq', 1];
					$where[] = ['pay_status', 'eq', 1];
					$where[] = ['end_date2', 'lt', date("Y-m-d H:i:s", time())];
					$where[] = ['curr_user_id', 'gt', 0];
				}
				if( isset($in['isDeliveryStatus']) && $in['isDeliveryStatus'] == 0 ){
//					待发货
					$where[] = ['delivery_status', 'eq', 0];
					$where[] = ['pay_status', 'eq', 1];
					$where[] = ['end_date2', 'lt', date("Y-m-d H:i:s", time())];
					$where[] = ['curr_user_id', 'gt', 0];
				}
				
				$data = Db::table("gd_auction") -> where($where) -> limit($start, $num) -> select();
				$count = Db::table("gd_auction") -> where($where) -> count();
				
				if( !empty($data) ){
					foreach ($data as $k => $v) {
						$data[$k]['createUserPhone'] = Db::table("gd_user") -> where(['id'=>$v['create_user_id']]) -> find()['phone'];
						$data[$k]['auctionSignup'] = Db::table("gd_auction_signup") -> where(['user_id'=>$v['curr_user_id'], 'auction_id'=>$v['id'], 'is_bond'=>1]) -> find();
						if( !empty($v['class_id1']) ) $data[$k]['calssName'] = Db::table("gd_classification") -> where(['id'=>$v['class_id1']]) -> find()['name'];
						if( !empty($v['class_id2']) ) $data[$k]['calssName'] = Db::table("gd_classification") -> where(['id'=>$v['class_id2']]) -> find()['name'];
						if( !empty($v['class_id3']) ) $data[$k]['calssName'] = Db::table("gd_classification") -> where(['id'=>$v['class_id3']]) -> find()['name'];
						if( $v['start_date'] <= date("Y-m-d H:i:s", time()) && $v['end_date2'] >= date("Y-m-d H:i:s", time()) ){
							$data[$k]['isInProgress'] = '进行中';
						}
						if( $v['start_date'] > date("Y-m-d H:i:s", time()) ){
							$data[$k]['isInProgress'] = '未开始';
						}
						if( $v['end_date2'] < date("Y-m-d H:i:s", time()) ){
							$data[$k]['isInProgress'] = '已结束';
						}
					}
				}
				
				$outArr['code'] = '1';
				$outArr['info'] = $data;
				$outArr['totalNums'] = $count;				
				
//				写入操作日志
				controller("service/OperLog") -> logs(
					[
						'title'=>'获取拍卖列表',
						'business_type'=>4,
						'oper_name'=>$userInfo['info']['nickname'],
						'oper_user_id'=>$userInfo['info']['id'],
						'json'=>[
							[
								'json_result'=>$outArr,
								'old_data'=> isset($old_data_json) ? $old_data_json : [],
							]
						]
					]
				);
			}
		}else{
			$outArr['code'] = '50001';
		}
		returnResults($outArr);
	}
	
	/**
	 * 编辑
	 */
	public function edit(){
		if(request() -> isPost()){
			$in = input("post.");
			$bool = is_empty(['user_id', 'id'], $in);
			if($bool){
				$outArr['code'] = '40004';
			}else{
				$userInfo = $this -> getUserInfo();
				
				$where[] = ['id', 'eq', $in['id']];
				$oldData = Db::table("gd_auction") -> where($where) -> find();
				if( empty($oldData) ) returnResults(['code'=>'-1', 'msg'=>'原数据有误']);
				if( $oldData['verify_status'] != 0 ) returnResults2('-1', '操作失败,只能编辑未审核的拍卖信息');
				
				$save = [];
				foreach ($in as $k => $v) {
					if( !in_array($k, ['id', 'user_id']) ){
						$save[$k] = is_array($v) ? json_encode($v, JSON_UNESCAPED_UNICODE) : $v;
					}
				}
				$save['end_date2'] = $save['end_date'];
				
				$save = filterArr($save, array_keys($save), $oldData);
				
				if( !empty($save) ){
					$re = Db::table("gd_auction") -> where($where) -> update($save);
					if( !$re ) returnResults2('-1', '编辑失败,请联系管理员');
				}
				$outArr['code'] = '1';
				
//				写入操作日志
				$old_data_json = $oldData;
				controller("service/OperLog") -> logs(
					[
						'title'=>'编辑拍卖',
						'business_type'=>3,
						'oper_name'=>$userInfo['info']['nickname'],
						'oper_user_id'=>$userInfo['info']['id'],
						'json'=>[
							[
								'json_result'=>$outArr,
								'old_data'=> isset($old_data_json) ? $old_data_json : [],
							]
						]
					]
				);
			}
		}else{
			$outArr['code'] = '50001';
		}
		returnResults($outArr);
	}
	
	/**
	 * 新增
	 */
	public function add(){
		if(request() -> isPost()){
			$in = input("post.");
			$bool = is_empty(['user_id'], $in);
			if($bool){
				$outArr['code'] = '40004';
			}else{
				$userInfo = $this -> getUserInfo();
				
				$newData = [];
				foreach ($in as $k => $v) {
					if( !in_array($k, ['id', 'user_id']) ){
						$newData[$k] = is_array($v) ? json_encode($v, JSON_UNESCAPED_UNICODE) : $v;
					}
				}
				$newData['create_user_id'] = $in['user_id'];
				$newData['create_user_name'] = $userInfo['info']['nickname'];
				$newData['create_user_type'] = $userInfo['info']['type'];
				$newData['create_date'] = date("Y-m-d H:i:s", time());
				$newData['end_date2'] = $newData['end_date'];
				$newData['curr_date'] = '0000-00-00 00:00:00';
				$newData['pay_date'] = '0000-00-00 00:00:00';
				$newData['waybill_date'] = '0000-00-00 00:00:00';
				
				$re = Db::table("gd_auction") -> insertGetId($newData);
				if( !$re ) returnResults2('-1', '新增失败,请联系管理员');
				$outArr['code'] = '1';
				$outArr['info'] = $re;
				
//				写入操作日志
				controller("service/OperLog") -> logs(
					[
						'title'=>'新增拍卖',
						'business_type'=>1,
						'oper_name'=>$userInfo['info']['nickname'],
						'oper_user_id'=>$userInfo['info']['id'],
						'json'=>[
							[
								'json_result'=>$outArr,
								'old_data'=> isset($old_data_json) ? $old_data_json : [],
							]
						]
					]
				);
			}
		}else{
			$outArr['code'] = '50001';
		}
		returnResults($outArr);
	}
	
}

?>